package itbour.onetouchshow.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.userinfo.VipDetailActivity;
import itbour.onetouchshow.activity.video.videoplay.VideoplayActivity;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.user.UserMakeTag;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.view.ActivityUtils;

/**
 * Created by onetouch on 2017/11/28.
 */

public class PaySuccessActivity extends MVPBaseActivity {
    public final int M = 1000;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //要做的事情
            handler.postDelayed(this, 3000);
            handler.removeCallbacks(runnable);
        }
    };
    private boolean isVipOrder;

    @Override
    public void loadSucceed(String result) {
    }

    @Override
    public void loadFaild(String error) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_success;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            isVipOrder = extras.getBoolean(AppConst.IS_VIP_ORDER);
        }
        L_.i("PaySuccessActivityonCreate isVipOrder" + isVipOrder);
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        L_.e("afterCreate");
        // handler.postDelayed(runnable, 3000);//每两秒执行一次runnable.
        idTvTime.setText(3 + "s后将跳转...");
        TimeCount timeCount = new TimeCount(5 * M, M);
        timeCount.start();
    }

    @Override
    public void finish() {
        super.finish();

        L_.e("finish" + isVipOrder);
        UserMakeTag userMakeTag;
        if (!isVipOrder) {
//            if (AppConst.BY_VIP_TAG.equals(AppConst.BUYXIUMI)) {
//                userMakeTag = UserMakeTag.BUYXIUMI;
//            } else {
//
//            }
            userMakeTag = UserMakeTag.REMOVEWATER;
            EventBus.getDefault().post(userMakeTag);

        } else {
            if (AppConst.BY_VIP_TAG.equals(VipDetailActivity.TAG_ME)) {

            } else {
                userMakeTag = UserMakeTag.BUYVIP;
                ActivityUtils.popAllActivityUntillOne(VideoplayActivity.class);
                EventBus.getDefault().post(userMakeTag);
            }

        }

    }

    public class TimeCount extends CountDownTimer {

        int count = 3;

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            // mBtnCode.setText(millisUntilFinished / 1000 + "s" + "后再发送");
            if (count == 0) {
                //setUserMakeTag(UserMakeTag.SETBG);
                finish();
            } else {
                if (idTvTime != null)
                    idTvTime.setText(count + "s后将跳转...");
            }
            count--;

        }

        @Override
        public void onFinish() {

        }
    }
}
